(function ($) {
    $.extend({
        dispatcher: function (successStop, errorContinue) {
            successStop = successStop || true;
            errorContinue = errorContinue || false;
            var isError = false;
            return {
                ajax: function (url, options) {
                    if (typeof url === "object") {
                        options = url;
                        url = undefined;
                    }
                    options = options || {};
                    var success = options.success, error = options.error;
                    options.success = function (data, textStatus, jqXHR) {
                        if (!isError || !successStop) {
                            success && success(data, textStatus, jqXHR);
                        }
                    };
                    options.error = function (jqXHR, textStatus, thrownError) {
                        if (!isError || errorContinue) {
                            isError = true;
                            error && error(jqXHR, textStatus, thrownError);
                        }
                    };
                    if (!isError || !successStop) {
                        $.ajax(url, options);
                    }
                },
                setError: function (error) {
                    isError = error;
                }
            }
        },
        template: function (template, object, pre) {
            if (!template || !object) {
                return template || "";
            }
            pre = pre || "";
            for (var i in object) {
                if (object.hasOwnProperty(i)) {
                    if (object[i] instanceof Object || object[i] instanceof Array) {
                        template = $.template(template, object[i], pre + i + ".")
                    } else {
                        template = template.replace(new RegExp("__" + pre + i + "__", "g"), object[i]);
                    }
                }
            }
            return template;
        },
        objToParam: function (object) {
            var array = [];
            for (var i in object) {
                if (object.hasOwnProperty(i)) {
                    array.push(i + "=" + object[i]);
                }
            }
            return array.join("&");
        },
        paramToObj: function (url) {
            if (!url) {
                url = window.location.href;
            }
            url = url.replace(/^.*\?/, "");
            var tmp = {};
            var params = url.split("&");
            for (var i in params) {
                var p = params[i].split("=");
                if (p.length > 1) {
                    tmp[p[0]] = p[1];
                }
            }
            return tmp;
        }
    });
    $.fn.extend({
        render: function (template, obj, model) {
            switch (model) {
                case "html": {
                    this.html($.template($(template).html(), obj));
                    break;
                }
                case "outer": {
                    this.attr("outerHTML", $.template($(template).html(), obj));
                    break;
                }
                case "prepend": {
                    this.prepend($.template($(template).html(), obj));
                    break;
                }
                case "before": {
                    this.before($.template($(template).html(), obj));
                    break;
                }
                case "after": {
                    this.after($.template($(template).html(), obj));
                    break;
                }
                case "replace": {
                    this.attr("outerHTML", $.template($(template).html(), obj));
                    break;
                }
                default: {
                    this.append($.template($(template).html(), obj))
                }

            }
        }
    });
})(jQuery);